package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.CourseTemplate;
import com.yuyou.fn.educational.entity.CourseType;
import com.yuyou.fn.educational.mapper.CourseTypeMapper;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.ICourseTemplateService;
import com.yuyou.fn.educational.service.ICourseTypeService;
import com.yuyou.fn.educational.vo.CourseTypeSaveOrUpdateVo;
import com.yuyou.fn.platform.permission.DataPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * CourseType 表数据服务层接口实现类
 *
 */
@Service
public class CourseTypeServiceImpl extends SuperServiceImpl<CourseTypeMapper, CourseType,Long> implements ICourseTypeService {

	@Resource
	private ICourseTemplateService courseTemplateService;

	@Resource
	private ICourseClassService courseClassService;


    @Override
    public String findCourseTypeNameById(Long courseTypeId) {

        Example<CourseType> example = CourseType.createExample();
        example.selectProperties("name");
        example.createCriteria().andEqualTo("courseTypeId",courseTypeId);
        CourseType courseType = findOneByExample(example);

        return courseType == null ? StringUtils.EMPTY : courseType.getName() ;
    }


	@Cacheable("cacheOfCourseTypeList")
    @Override
    public List<CourseType> findAllCourseTypeFromCache() {
        Example<CourseType> example = CourseType.createExample();
        return findListByExample(example);
    }

    @Override
    public CourseType findCourseTypeByName(String courseTypeName) {
        Example<CourseType> example = CourseType.createExample();
        example.createCriteria().andEqualTo("name",courseTypeName);
        return  findOneByExample(example);
    }

	@Override
	public Page<CourseType> findCourseTypeByQueryForManage(DataPermission dataPermission, int pageNo, int pageSize) {
		Example<CourseType> example = CourseType.createExample();
		example.orderBy("createTime").desc();
		return findPageByExample(example, pageNo, pageSize);
	}

	@CacheEvict(value = "cacheOfCourseTypeList",allEntries = true)
	@Transactional
	@Override
	public void saveOrUpdateCourseType(CourseTypeSaveOrUpdateVo courseTypeSaveOrUpdateVo) {
		CourseType courseType = new CourseType();
		BeanUtils.copyProperties(courseType, courseTypeSaveOrUpdateVo);
		checkClassTypeName(courseType);
		courseType.setUpdateTime(TimeUtils.now());
		if (null == courseType.getCourseTypeId()) {
			courseType.setCreateTime(TimeUtils.now());
			courseType.setAreaTeamId(0l);
			insertSelective(courseType);
		} else {
			//如果已经关联课程模版，则更新课程模版的课程类型名
			Example<CourseTemplate> example = CourseTemplate.createExample();
			example.createCriteria().andEqualTo("courseTypeId",courseType.getCourseTypeId());
			CourseTemplate courseTemplate = new CourseTemplate();
			courseTemplate.setCourseTypeName(courseType.getName());
			courseTemplateService.updateSelectiveByExample(courseTemplate,example);

			updateSelectiveById(courseType);
		}
	}

	private void checkClassTypeName(CourseType courseType) {
		if(courseType.getName().isEmpty()){
			throw new BusException("课程类型名不能为空");
		}

		Example<CourseType> example = CourseType.createExample();
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("name", courseType.getName().trim());
		CourseType one = findOneByExample(example);

		// 不管是新增还是编辑，课程类型名都不能重复
		if (one != null) {
			throw new BusException("课程类型已经存在");
		}
	}

	@CacheEvict(value = "cacheOfCourseTypeList",allEntries = true)
	@Transactional
	@Override
	public void deleteCourseType(Long courseTypeId) {
		Example<CourseTemplate> courseTemplateExample = CourseTemplate.createExample();
		courseTemplateExample.createCriteria().andEqualTo("courseTypeId", courseTypeId);
		long l = courseTemplateService.countByExample(courseTemplateExample);
		// 有课程模版在使用，则不能删除
		if (l > 0) {
			throw new BusException("课程类型在已被应用的情况下无法删除。");
		}
		/*
		 * List<Long> ids = BeanUtils.toList(courseTemplateList,
		 * "courseTemplateId"); if (null != ids && ids.size() > 0) {
		 * Example<CourseClass> courseClassExample =
		 * CourseClass.createExample();
		 * courseClassExample.createCriteria().andIn("courseTemplateId", ids);
		 * long l = courseClassService.countByExample(courseClassExample);
		 * 
		 * if (l > 0) { throw new BusException("课程类型已经被使用了，无法删除"); } }
		 */
		
		deleteById(courseTypeId);
	}
}